document.write(`<script type="text/javascript" src="/MapServer/Portal/libs/heatmap/heatmap.min.js"></script>`);

var heatMapFlag = false;

function heatMap() {
    if (!heatMapFlag) {
        heatMapWidget();
        heatMapFlag = true;
    } else {
        heatMapFlag = false;
        removeAllEntities();
    }
}

function heatMapWidget() {
    var heatData = getHeatData();
    //获取heatmap数据
    function getHeatData() {
        var polymerizationJsonDataArr = [];
        var returnData;
        $.ajaxSettings.async = false;
        $.getJSON("/portal/data/json/HeatmapData.json", function (data) {
            returnData = fun(data);
        })
        $.ajaxSettings.async = true;
        function fun(polymerizationJsonDataArr) {
            var namesArr = [];
            var dataObj = new Object();
            var dataArr = [];
            for (var i in polymerizationJsonDataArr) {
                if (i == "点集合") {
                    var childData = polymerizationJsonDataArr[i];
                    for (var j in childData) {
                        var obj = new Object();
                        obj.x = parseFloat(childData[j].x);
                        obj.y = parseFloat(childData[j].y);
                        obj.value = parseFloat(childData[j].value);
                        dataArr.push(obj);
                    }
                }
            }
            var max = 0;
            for (var i in dataArr) {
                max = Math.max(max, dataArr[i].value);
            }
            dataObj.max = max;
            dataObj.data = dataArr;
            return dataObj;
        }
        return returnData;
    }

    var coordinate = [113.0, 35.0, 119.0, 45.0];
    heatMapWidget3d(coordinate, heatData);
    
}


//三维热力图
function heatMapWidget3d(coordinate, heatData) {
    showHeatMap(coordinate, heatData);
    // 创建显示热力图
    function showHeatMap(coordinate, heatData) {
        var heatMap = createHeatMap(heatData.max + 1, heatData.data);
        createRectangle3d(coordinate, heatMap);
    }
    
    // 创建热力图
    function createHeatMap(max, data) {
        // 创建元素
        var heatDoc = document.createElement("div");
        heatDoc.setAttribute("id", "heatMapDiv");
        heatDoc.style.width = "1000px";
        heatDoc.style.height = "1000px";
        heatDoc.style.margin = "0px";
        heatDoc.style.display = "none";
        //width:1000px;height:1000px;margin: 0px;display: none;
        document.getElementById("mainBody").appendChild(heatDoc);
        // 创建热力图对象
        var heatmap = h337.create({
            container: heatDoc,
            radius: 20,
            maxOpacity: .9,
            minOpacity: 0.3,
            blur: .75,
            gradient: {
                '0.9': 'red',
                '0.8': 'orange',
                '0.7': 'yellow',
                '0.5': 'blue',
                '0.3': 'green',
            },
        });
        // 添加数据
        heatmap.setData({
            max: max,
            data: data
        });
        return heatmap;
    }   
    // 创建正方形 绑定热力图 
    function createRectangle3d(coordinate, heatMap) {
        viewer.entities.add({
            show: true,
            rectangle: {
                coordinates: Cesium.Rectangle.fromDegrees(coordinate[0], coordinate[1], coordinate[2], coordinate[3]),
                material: heatMap._renderer.canvas // 核心语句，填充热力图
            }
        });      
    }
}

